package client.controllers;

/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 * Animator.java
 *
 * Steven Louie Apr. 8, 2012
 */
import client.ActinoClient;
import client.models.ActinoPlayer;
import org.newdawn.slick.tiled.TiledMap;

public class Animator {

    public void animatePlayer(ActinoPlayer p, TiledMap m, int speed, int delta) {
        switch (p.dir) {
            case UP:
                p.py -= speed;
                if (p.py % (ActinoClient.SCALE_FACTOR * m.getTileWidth() / 4) == 0) {
                    if (++p.currentImageIndex >= p.frames.get("up").size()) {
                        p.currentImageIndex = 0;
                    }
                    p.currentImage = p.frames.get("up").get(p.currentImageIndex);
                }
                if (p.py % (ActinoClient.SCALE_FACTOR * m.getTileWidth()) == 0) {
                    p.animating = false;
                    p.currentImage = p.frames.get("up").get(0);
                }
                break;
            case DOWN:
                p.py += speed;
                if (p.py % (ActinoClient.SCALE_FACTOR * m.getTileWidth() / 4) == 0) {
                    if (++p.currentImageIndex >= p.frames.get("down").size()) {
                        p.currentImageIndex = 0;
                    }
                    p.currentImage = p.frames.get("down").get(p.currentImageIndex);
                }
                if (p.py % (ActinoClient.SCALE_FACTOR * m.getTileWidth()) == 0) {
                    p.animating = false;
                    p.currentImage = p.frames.get("down").get(0);
                }
                break;
            case RIGHT:
                p.px += speed;
                if (p.px % (ActinoClient.SCALE_FACTOR * m.getTileWidth() / 4) == 0) {
                    if (++p.currentImageIndex >= p.frames.get("right").size()) {
                        p.currentImageIndex = 0;
                    }
                    p.currentImage = p.frames.get("right").get(p.currentImageIndex);
                }
                if (p.px % (ActinoClient.SCALE_FACTOR * m.getTileWidth()) == 0) {
                    p.animating = false;
                    p.currentImage = p.frames.get("right").get(0);
                }
                break;
            case LEFT:
                p.px -= speed;
                if (p.px % (ActinoClient.SCALE_FACTOR * m.getTileWidth() / 4) == 0) {
                    if (++p.currentImageIndex >= p.frames.get("left").size()) {
                        p.currentImageIndex = 0;
                    }
                    p.currentImage = p.frames.get("left").get(p.currentImageIndex);
                }
                if (p.px % (ActinoClient.SCALE_FACTOR * m.getTileWidth()) == 0) {
                    p.animating = false;
                    p.currentImage = p.frames.get("left").get(0);
                }
                break;
        }
    }
}
